package com.gcxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gcxy.conf.result.PageVO;
import com.gcxy.conf.result.R;
import com.gcxy.conf.result.ResultCodeEnum;
import com.gcxy.dto.FenceDto;
import com.gcxy.entity.AccountInfo;
import com.gcxy.entity.Fence;
import com.gcxy.entity.Pen;
import com.gcxy.entity.Ranch;
import com.gcxy.mapper.FenceMapper;
import com.gcxy.service.FenceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiang
 * @since 2023-12-12
 */
@Service
public class FenceServiceImpl extends ServiceImpl<FenceMapper, Fence> implements FenceService {

    @Autowired
    private FenceMapper fenceMapper;

    @Override
    public R addFence(Fence fence) {
        //判断同一圈舍下围栏名称是否重复
        LambdaQueryWrapper<Fence> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Fence::getFenceName,fence.getFenceName())
                .eq(Fence::getPenId,fence.getPenId());
        Fence fenceName = this.baseMapper.selectOne(lqw);
        if(!ObjectUtils.isEmpty(fenceName)){
            return R.Failed(ResultCodeEnum.REQUEST_PARAM_ERROR.getCode(), "该围栏名称已存在");
        }
        //新增围栏
        this.baseMapper.insert(fence);
        return R.Success(fence);
    }

    @Override
    public R<Page<FenceDto>> getFenceByPage(FenceDto fenceDto) {
        QueryWrapper<FenceDto> qw = new QueryWrapper<>();
        //查询条件
        qw.eq(null != fenceDto.getRanchName(),"r.ranch_name",fenceDto.getRanchName())
                .eq(null != fenceDto.getRanchId(),"r.ranch_id",fenceDto.getRanchId())
                .eq(null != fenceDto.getPenId(),"p.pen_id",fenceDto.getPenId())
                .eq(null != fenceDto.getPenName(),"p.pen_name",fenceDto.getPenName())
                .like(null != fenceDto.getFenceName(),"f.fence_name",fenceDto.getFenceName())
                .eq(null != fenceDto.getIsEnable(),"f.is_enable",fenceDto.getIsEnable());
        //分页对象
        Page<FenceDto> fencePage = new Page(fenceDto.getPageNumber(), fenceDto.getPageSize());
        //查询
        fencePage = fenceMapper.selectFenceByPage(fencePage,qw);
        PageVO<FenceDto> fencePageVO = new PageVO();
        BeanUtils.copyProperties(fencePage, fencePageVO);
        return R.Success(fencePageVO);
    }

    @Override
    public R updateFence(Fence fence) {
        //判断围栏名称是否重复
        LambdaQueryWrapper<Fence> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Fence::getFenceName,fence.getFenceName())
                .eq(Fence::getPenId,fence.getPenId())
                .ne(Fence::getFenceId,fence.getFenceId());
        Fence fenceName = this.baseMapper.selectOne(lqw);
        if(!ObjectUtils.isEmpty(fenceName)){
            return R.Failed(ResultCodeEnum.REQUEST_PARAM_ERROR.getCode(),"该围栏名称已存在");
        }
        this.baseMapper.updateById(fence);
        return R.Success(fence);
    }

    @Override
    public R updateIsEnable(Fence fence) {
        LambdaUpdateWrapper<Fence> luw = new LambdaUpdateWrapper<>();
        luw.set(Fence::getIsEnable, 0).eq(Fence::getFenceId, fence.getFenceId());
        int row = this.baseMapper.update(fence, luw);
        return R.Success(row == 1 ? "停用成功" : "停用失败");
    }

}
